/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { FileSet, TaskDetails } from '@ohos/hvigor';
import path from 'path';

import { NodeCommandBuilder } from '../../builder/node-command-builder.js';
import { CodeType } from '../../enum/code-type-enum.js';
import { TaskNames } from '../common/task-names.js';
import { CompileNode } from '../compile-node.js';
import { TargetTaskService } from '../service/target-task-service.js';

import { packageJsonValidMainField } from './core-process-package-json.js';
import Task = TaskNames.Task;

export enum ObfuscateType {
  UGLIFY = 'uglify',
}

/**
 * Har编译arkTs相关源码
 */
export class HarCompileNode extends CompileNode {
  constructor(taskService: TargetTaskService, codeType: CodeType, taskDetails: TaskDetails) {
    super(taskService, codeType, taskDetails);

    // output assets dir
    this.aceModuleBuild = path.resolve(this.pathInfo.getInterMediatesLoaderOutPath());
  }

  declareInputFiles(): FileSet {
    const fileSet = super.declareInputFiles();

    // 此处获取的packageJsonValidMainField一定是存在和合法的
    const harExportMainEntryFilePath = path.resolve(this.moduleModel.getProjectDir(), packageJsonValidMainField!);

    fileSet.addEntry(harExportMainEntryFilePath);
    return fileSet;
  }

  protected async doTaskAction(): Promise<void> {
    await super.doTaskAction();
  }

  protected createNodeCommandBuilder() {
    const nodeCommandBuilder: NodeCommandBuilder = super.createNodeCommandBuilder();
    nodeCommandBuilder.addObfuscateType(ObfuscateType.UGLIFY);
    return nodeCommandBuilder;
  }
}
